
<template>
  <!-- 导入 -->
  <el-dialog
    title="导入数据"
    custom-class="rca_import_dialog"
    :visible="dialogImportVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :destroy-on-close="true"
    :width="step === '1' ? '50%' : '80%'"
  >
    <template v-if="step === '1'">
      <!-- <div class="import_label">数据来源：</div>
      <div class="import_value">
        <avue-radio
          v-model="bankType"
          :dic="dic"
          :disabled="uploading || step !== '1'"
        ></avue-radio>
      </div> -->
      <div class="import_label">上传文件：</div>
      <div
        v-if="file"
        class="import_value"
      >
        <i class="el-icon-document"></i>&nbsp;{{ file.name }}
      </div>
      <el-upload
        ref="upload"
        action="/"
        :auto-upload="false"
        :on-change="handleChange"
        :disabled="uploading"
      >
        <el-button
          :disabled="uploading"
          slot="trigger"
          size="small"
          type="primary"
        >{{file ? '重新选取文件':'选取文件'}}</el-button>
        <div
          slot="tip"
          class="el-upload__tip"
        >
          只能上传xls/xlsx/csv文件，且不超过20MB
        </div>
      </el-upload>
      <el-button
        @click="handleDownloadClick"
        type="text"
        size="small"
      >导出模板</el-button>
    </template>
    <TempData
      v-else
      :bankType="bankType"
    />
    <div
      slot="footer"
      class="dialog-footer"
    >
      <template v-if="step === '1'">
        <el-button
          :loading="uploading"
          @click="handleCancel"
        >取 消</el-button>
        <el-button
          :loading="uploading"
          type="primary"
          @click="handleUpload"
        >确 定</el-button>
      </template>
      <template v-else>
        <el-button
          :loading="uploading"
          @click="handleCancelData"
        >取 消</el-button>
        <el-button
          :loading="uploading"
          type="primary"
          @click="handleConfirmData"
        >确 定</el-button>
      </template>
    </div>
  </el-dialog>
</template>

<script>
import { fetchList, tmp } from "@/api/rca/swckbackelectronicbill";
import { bankDic } from "@/const/crud/rca/swckbackelectronicbill";
import TempData from "./TempData";
import { handleExcelResponse } from "@/util/util";

export default {
  name: "ImportData",
  components: {
    TempData,
  },
  data () {
    return {
      bankType: "ccb",
      dic: bankDic,
      uploading: false,
      file: null,

      step: "1",
    };
  },
  watch: {
    dialogImportVisible (val) {
      if (val === false) {
        this.file = null;
        this.step = '1';
      } else {
        this.bankType = this.defaultBank;
      }
    }
  },
  props: ["dialogImportVisible", "defaultBank"],
  methods: {
    handleChange (file, fileList) {
      this.file = file.raw;
      this.$refs.upload.clearFiles();
    },

    handleClose () {
      this.$emit("close");
    },
    handleCancel () {
      this.handleClose();
    },
    async handleUpload () {
      if (!this.file) {
        this.$message.error("请选择上传文件");
        return;
      }
      this.uploading = true;
      const formData = new FormData();
      formData.append("file", this.file);
      try {
        const { data } = await tmp[this.bankType].upload(formData);
        if (data) {
          this.$message.success("上传成功");
          this.step = "2";
        }
      } finally {
        this.uploading = false;
      }
    },
    async handleCancelData () {
      this.$confirm('此操作将撤销当前上传数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.uploading = true;
        try {
          await tmp[this.bankType].cancel();
          this.$message.success("操作成功");
          this.handleClose();
        } finally {
          this.uploading = false;
        }
      })
    },
    async handleConfirmData () {
      this.$confirm('当前上传数据将立即生效, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.uploading = true;
        try {
          await tmp[this.bankType].save();
          this.$message.success("操作成功");
          this.handleClose();
        } finally {
          this.uploading = false;
        }
      })
    },
    async handleDownloadClick () {
      this.uploading = true;
      try {
        const res = await tmp[this.bankType].template();
        // handleExcelResponse(res, `模板.xlsx`);
        handleExcelResponse(res);
      } finally {
        this.uploading = false;
      }
    },
  },
};
</script>
<style lang="scss">
.rca_import_dialog {
  .import_label {
    margin-bottom: 20px;
    font-size: 16px;
    font-weight: 600;
  }
  .import_value {
    margin-bottom: 20px;
  }
}
</style>
